package nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class SelectorDemo {

	public static void main(String[] args) throws Exception {
		t1();
	}

	public static void t1() throws Exception {
		ServerSocketChannel ssc = ServerSocketChannel.open();
		ssc.socket().bind(new InetSocketAddress(8989));
		ssc.configureBlocking(false);
		Selector selector = Selector.open();
		ssc.register(selector, SelectionKey.OP_ACCEPT);
	       while (true) {
	            if (selector.select() > 0) { // 如果注册的事件发生
	                Set<SelectionKey> selectedKeys = selector.selectedKeys(); // 获取发生的事件
	                Iterator<SelectionKey> it = selectedKeys.iterator(); // 依次进行处理
	                while (it.hasNext()) {
	                    SelectionKey selectionKey = it.next();
	                    if (selectionKey.isAcceptable()) { // 如果是Accept事件
	                        // 获取注册的ServerSocketChannel
	                    	ServerSocketChannel serverSocketChannel = ((ServerSocketChannel) selectionKey.channel()); 
	                        SocketChannel socketChannel = serverSocketChannel.accept(); // 建立连接
	                        socketChannel.configureBlocking(false);
	                        socketChannel.register(selector, SelectionKey.OP_READ); // 注册该连接的Read事件
	                        System.out.println("Connected: " + socketChannel.socket().getRemoteSocketAddress());
	                    } else if (selectionKey.isReadable()) { // 如果是Read事件
	                        // 获取注册的SocketChannel
	                        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
	                        ByteBuffer buffer = ByteBuffer.allocate(1024);
	                        while (socketChannel.read(buffer) > 0) { // 读取接收到的数据
	                            buffer.flip();
	                            byte[] dst = new byte[buffer.limit()];
	                            buffer.get(dst);
	                            System.out.println(new String(dst)+"server");
	                        }
	                        buffer.clear();
	                        socketChannel.register(selector, SelectionKey.OP_WRITE);
	                    }else if(selectionKey.isWritable()){
	                    	System.out.println("writable");
	                    	SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
	                    	ByteBuffer buffer=ByteBuffer.wrap("good".getBytes());
	                    	socketChannel.write(buffer);
	                    	socketChannel.register(selector, SelectionKey.OP_READ);
	                    	//socketChannel.finishConnect();
	                    }
	                    it.remove(); // 需要将处理过的事件移除
	                }
	            }
	            Thread.sleep(100);
	        }
	}

}
